package cn.itcast.usermanage.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.abel533.entity.Example;
import com.github.abel533.entity.Example.Criteria;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.itcast.usermanage.mapper.NewUserMapper;
import cn.itcast.usermanage.mapper.UserMapper;
import cn.itcast.usermanage.pojo.EasyUIResult;
import cn.itcast.usermanage.pojo.User;
import cn.itcast.usermanage.service.UserService;

@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private NewUserMapper userMapper;
	@Override
	public EasyUIResult queryUserAllByPage(int page, int rows) {
		PageHelper ph=new PageHelper();
		EasyUIResult result = new EasyUIResult();
		ph.startPage(page, rows);
	//List<User> list=userMapper.queryUserByPage((page-1)*rows,rows);
	//查询所有
	List<User> userList=userMapper.select(new User());
	PageInfo<User> info = new PageInfo<>(userList);
		result.setRows(userList);
		result.setTotal(info.getTotal());
		return result;
	}
	@Override
	public void addUserTest(User user1, User user2) {
		userMapper.insertSelective(user1);
		
		userMapper.insert(user2);
	}
	@Override
	public int addUser(User user) {
		int i = userMapper.insertSelective(user);
		return i;
	}
	@Override
	public int deleteUserById(long id) {
	
		return userMapper.deleteByPrimaryKey(id);
	}
	@Override
	public User queryUserById(long id) {
		// TODO Auto-generated method stub
		return userMapper.selectByPrimaryKey(id);
	}
	@Override
	public int editUser(User user) {
		Example example=new Example(User.class);
		Criteria c = example.createCriteria();
		c.andEqualTo("id", user.getId());
		return userMapper.updateByExampleSelective(user, example);
		
		//return userMapper.updateByPrimaryKeySelective(user);
		
	}
	@Override
	public void deleteUserByIds(List<Object> ids) {
		
		Example example=new Example(User.class);
		Criteria c = example.createCriteria();
		c.andIn("id", ids);
		 userMapper.deleteByExample(example);
	}
	

}
